#include<bits/stdc++.h>
using namespace std;
// #define int long long
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
using vi=vector<int>;
using pi=pair<int,int>;

const int N=2e5+5, LG=15, S=1<<LG, R=5;

int n, q, c;
int a[N], fa[N];
vi G[N];

int dfn[N], dfc, sz[N], hv[N], bz[18][N], bs[18][N];

void dfs0(int u){
	sz[u]=1;
	for(int v:G[u]){
		dfs0(v);
		sz[u]+= sz[v];
		if(sz[v]>sz[hv[u]]){
			hv[u]=v;
		}
	}
}
void dfs1(int u){
	dfn[u]=++dfc;
	if(hv[u]){
		dfs1(hv[u]);
	}
	for(int v:G[u]){
		if(v!=hv[u]){
			dfs1(v);
		}
	}
}

vector<pi> msg[N], info[N];
int ans[N];

int f[S];
void add(int x,int w){
	int y=x&31, r=y;
	x-= y;
	do{
		f[x|r]+= w;
	}while( (r=(r+1)|y) < 32);
}
int sum(int x){
	int y=x&(~31), r=y, res=0;
	x-= y;
	do{
		res+= f[x|r];
	}while( (r=(r-1)&y) != y );
	return res;
}

void dfs2(int u){
	for(int v:G[u]){
		if(v==hv[u]){
			continue;
		}
		dfs2(v);
		rep(i,dfn[v],dfn[v]+sz[v]-1){
			for(auto [x,w]:info[i]){
				add(x, -w);
			}
		}
	}
	if(hv[u]){
		dfs2(hv[u]);
		for(int v:G[u]){
			if(v==hv[u]){
				continue;
			}
			rep(i,dfn[v],dfn[v]+sz[v]-1){
				for(auto [x,w]:info[i]){
					add(x, w);
				}
			}
		}
	}
	for(auto [x,w]:info[dfn[u]]){
		add(x, w);
	}
	for(auto [s,id]:msg[u]){
		ans[id]=sum(s);
	}
}

signed main(){
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(0);
	
	cin>>n>>q>>c;
	rep(i,1,n){
		cin>>a[i];
	}
	rep(i,2,n){
		cin>>fa[i];
		G[fa[i]].pb(i);
	}
	dfs0(1);
	dfs1(1);
	
	rep(i,1,n){
		bz[0][i]=fa[i];
		bs[0][i]=1<<a[i];
	}
	rep(i,1,17){
		rep(j,1,n){
			int f=bz[i-1][j];
			bz[i][j]=bz[i-1][f];
			bs[i][j]=bs[i-1][j] | bs[i-1][f];
		}
	}
	
	rep(i,1,n){
		int u=i, s=1<<a[u];
		while(u){
			int v=u;
			per(j,17,0){
				if((bs[j][v]|s)==s){
					v=bz[j][v];
				}
			}
			info[ dfn[u] ].pb(s, 1);
			info[ dfn[v] ].pb(s, -1);
			u=v, s|=1<<a[v];
		}
	}
	rep(i,1,q){
		int s,u; cin>>s>>u;
		if(!(s>>a[u]&1)){
			ans[i]=1;
			continue;
		}
		int v=u;
		per(j,17,0){
			if((bs[j][v]|s)==s){
				v=bz[j][v];
			}
		}
		per(j,17,0){
			if(dfn[bz[j][u]]>dfn[v]){
				u=bz[j][u];
			}
		}
		msg[u].pb(s, i);
	}
	
	dfs2(1);
	
	rep(i,1,q){
		cout<< ans[i] <<'\n';
	}
}